package com.sz.mes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.vo.JsonResult;
import com.diboot.iam.entity.IamUser;
import com.diboot.iam.mapper.IamUserMapper;
import com.diboot.iam.util.IamSecurityUtils;
import com.sz.mes.dto.ReceiveDTO;
import com.sz.mes.entity.Outbound;
import com.sz.mes.entity.Receive;
import com.sz.mes.entity.warehouse.OutboundRecord;
import com.sz.mes.mapper.OutboundMapper;
import com.sz.mes.mapper.ReceiveMapper;
import com.sz.mes.mapper.warehouse.LatticeGoodsMapper;
import com.sz.mes.mapper.warehouse.OutboundRecordMapper;
import com.sz.mes.service.ReceiveService;
import com.sz.mes.service.impl.BaseCustomServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 签收相关Service实现
 *
 * @author hem
 * @version 1.0
 * @date 2023-06-29
 * Copyright © szhs
 */
@Service
@Slf4j
public class ReceiveServiceImpl extends BaseCustomServiceImpl<ReceiveMapper, Receive> implements ReceiveService {
    private final ReceiveMapper receiveMapper;
    private final OutboundRecordMapper outboundRecordMapper;
    private final IamUserMapper iamUserMapper;
    private final OutboundMapper outboundMapper;

    public ReceiveServiceImpl(ReceiveMapper receiveMapper, OutboundRecordMapper outboundRecordMapper,
                              IamUserMapper iamUserMapper,OutboundMapper outboundMapper) {
        this.receiveMapper = receiveMapper;
        this.outboundRecordMapper = outboundRecordMapper;
        this.iamUserMapper = iamUserMapper;
        this.outboundMapper = outboundMapper;
    }


    @Override
    public JsonResult updateReceive(ReceiveDTO receiveDTO) {
        QueryWrapper<Receive> receiveQueryWrapper = new QueryWrapper<>();
        receiveQueryWrapper.eq("outbound_num", receiveDTO.getOutboundNum());
//        receiveQueryWrapper.eq("goods_code", receiveDTO.getGoodsCode());
        Receive receive = receiveMapper.selectOne(receiveQueryWrapper);
        Long currentUserId = IamSecurityUtils.getCurrentUserId();
        IamUser iamUser = iamUserMapper.selectById(currentUserId);
        receive.setReciveUserName(iamUser.getRealname());
        receive.setIsRecive(true);
        receiveMapper.updateById(receive);

        //出库表回显签收人
        QueryWrapper<Outbound> outboundQueryWrapper = new QueryWrapper<>();
        outboundQueryWrapper.eq("outbound_num",receiveDTO.getOutboundNum());
        Outbound outbound = outboundMapper.selectOne(outboundQueryWrapper);
        outbound.setReceiveUserName(iamUser.getRealname());
        outboundMapper.updateById(outbound);

        LambdaUpdateWrapper<OutboundRecord> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(OutboundRecord::getOutboundId, receive.getOutboundId()).set(OutboundRecord::getReciveUserId, IamSecurityUtils.getCurrentUserId());
        outboundRecordMapper.update(null, lambdaUpdateWrapper);
        return JsonResult.OK(receive);
    }
}
